/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.sober.util;

import com.jspx.utils.StringUtil;
import com.jspx.utils.NumberUtil;
import com.jspx.utils.DateUtil;
import com.jspx.utils.HtmlUtil;
import java.util.Map;
import java.util.Date;
import java.util.HashMap;
import java.io.Serializable;

/**
 * Created by IntelliJ IDEA.
 * User:chenYuan (mail:39793751@qq.com)
 * Date: 2006-4-14
 * Time: 0:41:16
 */
public class DataMap<K extends String,V> extends HashMap<K ,V> implements Serializable
{
    public DataMap()
    {

    }

    /**
     * 得到Double类型
     *
     * @param fname 字段名称
     * @return String
     */
    public double getDouble(String fname)
    {
        Object o = get(fname);
        if (o == null) return 0;
        return (Double) o;
    }


    public float getFloat(String fname)
    {
        Object o = get(fname);
        if (o == null) return 0;
        return Float.valueOf(o.toString());
    }

    /**
     * 得到Double类型
     *
     * @param fname 字段名称
     * @param scale 保留几位
     * @return String
     */

    public double getDouble(String fname, int scale)
    {
        return NumberUtil.getRound(getDouble(fname), scale).doubleValue();
    }

    /**
     * 得到Int类型
     *
     * @param fname 字段名称
     * @return String
     */
    public int getInt(String fname)
    {
        Object o = get(fname);
        if (o == null) return 0;
        return StringUtil.toInt(o.toString());
    }

    public long getLong(String fname)
    {
        Object o = get(fname);
        if (o == null) return 0;
        return StringUtil.toLong(o.toString());
    }
    

    public String getValue(String fname)
    {
        Object o = get(fname);
        if (o == null) return StringUtil.empty;
        String s = o.toString();
        if (StringUtil.isNULL(s)) return StringUtil.empty;
        return s;
    }

    /**
     * 得到boolean 类型
     *
     * @param fname
     * @return boolean
     */
    public boolean getBoolean(String fname)
    {
        return StringUtil.toBoolean(getValue(fname));
    }

    /**
     * 判断条件输出结果
     *
     * @param fname
     * @param strue
     * @param sfalse
     * @return String
     */
    public String getBoolean(String fname, String strue, String sfalse)
    {
        if (getBoolean(fname)) return strue;
        return sfalse;
    }


    /**
     * 得到Date类型
     *
     * @param fname
     * @param dateformat
     * @return String
     */
    public String getDateString(String fname, String dateformat)
    {
        Object o = get(fname);
        if (o == null) return StringUtil.empty;
        Date date = (Date) o;
        return DateUtil.toString(date,dateformat);
    }

    /**
     * 得到Date类型
     *
     * @param fname
     * @return String
     */
    public Date getDate(String fname)
    {
        Object o = get(fname);
        if (o == null) return DateUtil.empty;
        Date date = (Date) o;
        return date;
    }

    public String getValue(String fname, int length)
    {
        return getValue(fname, length, false, "");
    }

    public String getValue(String fname, int length, boolean nohtml)
    {
        return getValue(fname, length, nohtml, "");
    }

    public String getValueNewlines(String fname)
    {
        return StringUtil.toBrLine(getValue(fname));
    }

    /**
     * 输出图片
     *
     * @param fname
     * @param width
     * @param height
     * @return String
     */
    public String getImageHtml(String fname, int width, int height)
    {
        Object o = get(fname);
        if (o == null) return StringUtil.empty;
        String result = (String) o;
        if (StringUtil.isNULL(result)) return StringUtil.empty;
        StringBuilder sb = new StringBuilder();
        sb.append("<img src=").append(result).append(" border=0 ");
        if (width > 0)
        {
            sb.append("width=").append(NumberUtil.toString(width)).append(" ");
        }

        if (width > 0)
        {
            sb.append("height=").append(NumberUtil.toString(height)).append(" ");
        }
        sb.append("/>");
        return sb.toString();
    }

    /**
     * 输出图片
     *
     * @param fname
     * @param width
     * @param height
     * @return String
     */
    public String getImageHtml(String fname, int width, int height, String path)
    {
        Object o = get(fname);
        if (o == null) return StringUtil.empty;
        String result = (String) o;
        if (StringUtil.isNULL(result)) return StringUtil.empty;
        result = path + result;
        StringBuilder sb = new StringBuilder();
        sb.append("<img src=").append(result).append(" border=0 ");
        if (width > 0)
        {
            sb.append("width=").append(NumberUtil.toString(width)).append(" ");
        }

        if (width > 0)
        {
            sb.append("height=").append(NumberUtil.toString(height)).append(" ");
        }
        sb.append("/>");

        return sb.toString();
    }

    /**
     * @param fname     字段名称
     * @param length    长度
     * @param nohtml    是否取出html
     * @param addstring 尾巴
     * @return String
     */
    public String getValue(String fname, int length, boolean nohtml, String addstring)
    {
        Object o = get(fname);
        if (o == null) return StringUtil.empty;
        String result = (String) o;
        if (nohtml) result = HtmlUtil.deleteHtml(result);
        if (result.length() > length + 2)
        {
            result = result.substring( 0, length) + addstring;
        }
        return result;
    }

    public V get(java.lang.Object o)
    {
        String key = (String) o;
        if (key==null) return null;
        return super.get(key.toLowerCase());
    }

    @SuppressWarnings("unchecked")
    public V put(K  k, V v)
    {
        String key = k.toLowerCase();
        return super.put((K) key, v);
    }

    @SuppressWarnings("unchecked")
    public V remove(java.lang.Object o)
    {
        String key = (String) o;
        return super.remove(key.toLowerCase());
    }

    @SuppressWarnings("unchecked")
    public void putAll(java.util.Map<? extends K, ? extends V> inmap)
    {
        Map<K,V>  xmap =  (Map<K,V>)inmap;
        for (String myk : xmap.keySet())
        {
            Object o = xmap.remove(myk);
            super.put((K)myk.toLowerCase(),(V)o);
        }
    }
}